package com.fwzc.giuseppe.activity;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.adapter.AllOrderAdapter;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.entity.Order;
import com.fwzc.giuseppe.util.OrderOperation;

public class AllOrderActivity extends BaseActivity implements OnClickListener,OnRefreshListener {
	private ListView myListView;
	private AllOrderAdapter adapter;
	private ArrayList<Order> datas = new ArrayList<Order>();
	private SwipeRefreshLayout refreshLayout;
	@SuppressLint("HandlerLeak") 
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what == 1){
				refreshLayout.setRefreshing(false);
			}
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_all_order);

		initBar();
		initView();
		initListView();
	}

	private void initView() {
		actionbar_side_name.setText("全部订单");
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		iv_actionbar_side_left.setOnClickListener(this);

		myListView = (ListView) findViewById(R.id.all_order);
		refreshLayout = (SwipeRefreshLayout)findViewById(R.id.refreshLayout);
		refreshLayout.setOnRefreshListener(this);
		refreshLayout.setColorSchemeResources(android.R.color.holo_green_dark,android.R.color.holo_green_light,android.R.color.holo_orange_light,android.R.color.holo_red_light);
	}

	private void initListView() {
		for (int i = 0; i < 12; i++) {
			Order order = new Order();
			switch (i % 4) {
			case 0:
				order.operation = OrderOperation.WAINT_PAYMENT;
				break;
			case 1:
				order.operation = OrderOperation.WAIT_RECEIVE;
				break;
			case 2:
				order.operation = OrderOperation.WAINT_COMMENT;
				break;
			case 3:
				order.operation = OrderOperation.CHANGE_RETURN;
				break;
			default:
				break;
			}
			datas.add(order);
		}
		adapter = new AllOrderAdapter(this, datas);
		myListView.setAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_actionbar_side_left:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onRefresh() {
		refreshLayout.setRefreshing(true);
		handler.sendEmptyMessage(1);
	}
}
